package comtacti.native.tests;
import utest.Assert;
import comtacti.native.AutoCffi;
import comtacti.native.enums.ValuedEnum;
import comtacti.native.options.Types;

using comtacti.native.tests.TestCffiWithEnum;
using comtacti.native.enums.ValuedEnum;


class TestCffiWithEnum
{
	public function new()
	{
		
	}
	
	public function setup():Void 
	{
		
	}
	
	public function test_CffiValuedInt():Void
	{
		Assert.equals(10 * 10, CffiInlinedCpp.renamedHelperFunction(ValueTen));
		Assert.equals(20 * 20, CffiInlinedCpp.renamedHelperFunction(ValueTwenty));
		Assert.equals(0, CffiInlinedCpp.renamedHelperFunction(ValueZero));
	}
	
	public function test_CffiValuedString():Void
	{
		Assert.isTrue(CffiInlinedCpp.isHelloWorld(ValueHello, ValueWorld) != 0, "Error when sending enum with a string value to the CFFI.");
	}
	
	public function test_CffiBitEnum():Void
	{
		Assert.equals(0x101 * 0x101, CffiInlinedCpp.renamedHelperFunction2(ValueFirst.toBitEnum().and(ValueThird)));
	}
}


@:cffi("ngate_test",type = Eager)
@:include("<stdio.h>")
@:include("<string.h>")
private class CffiInlinedCpp extends AutoCffi
{
	private static function __init__():Void 
	{
		untyped __cffi__('
		int helper_function(int i);
		');
	}
	
	@:native("helper_function")
	public static function renamedHelperFunction(intEnum:ValuedIntEnum):Int;
	
	@:native("helper_function")
	public static function renamedHelperFunction2(intEnum:Array<ValuedBitEnum>):Int;
	
	public static function isHelloWorld(part1:ValuedStringEnum, part2:ValuedStringEnum):Int
	{
		return untyped __cffi__('
			return alloc_int((strcmp(part1, "Hello") == 0) && (strcmp(part2, "World") == 0)); //c++ cffi seems to have a problem with alloc_bool
		');
	}
}


@:helper(comtacti.native.tests.ValuedIntHelper2)
@:values(Int) private enum ValuedIntEnum
{
	@:value(10) ValueTen;
	@:value(20) ValueTwenty;
	@:value(0) ValueZero;
}

@:subject(ValuedIntEnum) class ValuedIntHelper2 extends ValuedEnum
{
	
}

@:helper(comtacti.native.tests.ValuedManyHelper2)
@:values(String) private enum ValuedStringEnum
{
	@:value("Hello") ValueHello;
	@:value("World") ValueWorld;
}

@:helper(comtacti.native.tests.ValuedManyHelper2)
@:values(Bit) private enum ValuedBitEnum
{
	@:value(0x1) ValueFirst;
	@:value(0x10) ValueSecond;
	@:value(0x100) ValueThird;
}

@:subject(ValuedBitEnum)
@:subject(ValuedStringEnum)
class ValuedManyHelper2 extends ValuedEnum
{
	
}